<template>
  <!-- 编码生成规则明细表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="规则主键" prop="ruleId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.ruleId"
          placeholder="请输入规则主键"
          clearable
        />

        <span v-else>{{ formData.ruleId }}</span>
      </el-form-item>
      <el-form-item label="序号" prop="seq">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.seq"
          placeholder="请输入序号"
          clearable
        />

        <span v-else>{{ formData.seq }}</span>
      </el-form-item>
      <el-form-item label="编码段类型" prop="codeSectionType">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.codeSectionType"
          placeholder="请选择编码段类型"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('CodeSectionType')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName(
            "CodeSectionType",
            formData.codeSectionType
          )
        }}</span>
      </el-form-item>
      <el-form-item label="编码长度" prop="codeLength">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.codeLength"
          placeholder="请输入编码长度"
          clearable
        />

        <span v-else>{{ formData.codeLength }}</span>
      </el-form-item>
      <el-form-item label="编码格式" prop="codeFormat">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.codeFormat"
          placeholder="请输入编码格式"
          clearable
        />

        <span v-else>{{ formData.codeFormat }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SYSTEMSYSCODEROLEITEMADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  ruleId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  seq: [],
  codeSectionType: [
    { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" },
  ],
  codeLength: [],
  codeFormat: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  ruleId: "",
  seq: "",
  codeSectionType: "",
  codeLength: "",
  codeFormat: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const { id, ruleId, seq, codeSectionType, codeLength, codeFormat } =
        newVal;
      Object.assign(formData, {
        id,
        ruleId,
        seq,
        codeSectionType,
        codeLength,
        codeFormat,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.ruleId = "";
  formData.seq = "";
  formData.codeSectionType = "";
  formData.codeLength = "";
  formData.codeFormat = "";
}

const loading = ref(false);

/** 编码生成规则明细保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/syscoderoleitem/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
